package im.yisme.view.ui.animation;

import android.R.integer;
import im.yisme.view.ui.animation.pojo.GameResultDataPOJO;

public class GameResultData extends Thread{

	private GameResultDataPOJO data;
	private int realScore;
	private float realAccuracy;
	private int realMaxCombo;
	private String realLevel;
	
	private static final int ANIMATION_UPDATE_INTERVAL = 20;
	private static final int ANIMATION_UPDATE_TIMES = 100;
	private int count;
	
	public GameResultData(GameResultDataPOJO data,int score, float accuracy, int maxCombo, String level){
		this.data = data;
		realScore = score;
		realAccuracy = accuracy;
		realMaxCombo = maxCombo;
		realLevel = level;
		count = 0;
	}
	public void run(){
		
		int scorePiece = realScore / ANIMATION_UPDATE_TIMES +1;
		float accuracyPiece = realAccuracy / ANIMATION_UPDATE_TIMES ;
		int maxComboPiece = realMaxCombo / ANIMATION_UPDATE_TIMES +1;
		
		while(count < ANIMATION_UPDATE_TIMES){
			
			try {
				
				if(data.getScore() >= realScore){
					data.setScore(realScore);
				}
				else {
					data.setScore(data.getScore()+scorePiece);
				}
				
				if(data.getAccuracy() >= realAccuracy){
					data.setAccuracy(realAccuracy);
				}
				else {
					data.setAccuracy(data.getAccuracy()+accuracyPiece);
				}
				
				if(data.getMaxCombo() >= realMaxCombo){
					data.setMaxCombo(realMaxCombo);
				}
				else {
					data.setMaxCombo(data.getMaxCombo()+maxComboPiece);
				}
				
				count++;
				Thread.sleep(ANIMATION_UPDATE_INTERVAL);
				
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		data.setScore(realScore);
		data.setMaxCombo(realMaxCombo);
		data.setAccuracy(realAccuracy);
		
		count = 0;
		data.setLevel(realLevel);
		
		float sizePiece = (float) (100.0/ANIMATION_UPDATE_TIMES);
		int alphaPiece = 255/ANIMATION_UPDATE_TIMES;
		while(count < ANIMATION_UPDATE_TIMES){
			try {
				
				data.setLevelSize(data.getLevelSize()-sizePiece);
				data.setLevelAlpha(data.getLevelAlpha()+alphaPiece);
	
				
				count++;
				Thread.sleep(ANIMATION_UPDATE_INTERVAL-15);
				
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		data.setLevelSize(30);
		data.setLevelAlpha(255);
	}
}
